﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace MyCadDll.MyObject
{
    public class MyLine : MyCadObject, IComparable
    {
        MyPoint m_p1;

        public MyPoint P1
        {
            get { return m_p1; }
            set { m_p1 = value; }
        }
        MyPoint m_p2;

        public MyPoint P2
        {
            get { return m_p2; }
            set { m_p2 = value; }
        }
        public List<MyText> leftTextList = new List<MyText>();
        public List<MyText> rightTextList = new List<MyText>();
        public List<MyText> topTextList = new List<MyText>();
        public List<MyText> bottomTextList = new List<MyText>();
        public MyLine(MyPoint p1, MyPoint p2)
        {
            if (p1.P[0] < p2.P[0])
            {
                m_p1 = p1;
                m_p2 = p2;
            }
            else
            {
                if (p1.P[0] == p2.P[0])
                {
                    if (p1.P[1] < p2.P[1])
                    {
                        m_p1 = p1;
                        m_p2 = p2;
                    }
                    else
                    {
                        m_p2 = p1;
                        m_p1 = p2;
                    }
                }
                else
                {
                    m_p2 = p1;
                    m_p1 = p2;
                }
            }
        }
        public MyLine()
        {
            P1 = new MyPoint();
            P2 = new MyPoint();
        }
        public int CompareTo(object obj)
        {
            MyLine line;
            if (obj is MyLine)
            {
                line = obj as MyLine;
                return P1.CompareTo(line.P1);
            }
            if (obj is MyText)
            {

            }
            throw new NotImplementedException();
        }
        public override  int CompareTo_X(object obj)
        {
            MyLine line;
            if (obj is MyLine)
            {
                line = obj as MyLine;
                return P1.CompareTo_X(line.P1);
            }
          
            throw new NotImplementedException();
        }
        public  override int CompareTo_Y(object obj)
        {
            return CompareTo(obj);
        }

        public bool PointInSect(MyPoint p)
        {
            if (P1.X == P2.X)
            {
                if (P1.Y == P2.Y)
                {
                    return false;
                }
                else
                {
                    if (p.Y >= Math.Min(P1.Y,P2.Y) && p.Y <=Math.Max (P2.Y,P1.Y))
                        return true;
                }
            }
            else
            {
                if (P1.Y == P2.Y)
                {
                    if (P1.X == P2.X)
                    {
                        return false;
                    }
                    else
                    {
                        if (p.X >=Math.Min(P1.X,P2.X) && p.X <=Math.Max(P2.X,P1.X))
                            return true;
                    }
                }
                else
                {
                    if (p.Y >=Math.Min(P1.Y,P2.Y) && p.Y <=Math.Max(P2.Y,P1.Y)&&
                        p.X>=Math.Min(P1.X,P2.X)&&p.X<=Math.Max(P2.X,P1.X))
                        return true;
                }
            }
            return false;
        }
        public bool isEmpty()
        {
            if (P1.X == P1.Y&&P1.Y == P1.Z&&P1.Z == P2.X&&
                P2.X == P2.Y&&P2.Y == P2.Z)
            {
                return true;
            }
            return false;
        }
        private double GetMinPx()
        {
            if (m_p1.P[0] < m_p2.P[0])
                return m_p1.P[0];
            else
                return m_p2.P[0];
        }
        private double GetMaxPx()
        {
            if (m_p1.P[0] >m_p2.P[0])
                return m_p1.P[0];
            else
                return m_p2.P[0];
        }
        private double GetMinPy()
        {
            if (m_p1.P[1] < m_p2.P[1])
                return m_p1.P[1];
            else
                return m_p2.P[1];
        }
        private double GetMaxPy()
        {
            if (m_p1.P[1] >m_p2.P[1])
                return m_p1.P[1];
            else
                return m_p2.P[1];
        }
        public int CompareTo(object obj,bool bx)
        {
            if (obj is MyLine)
            {
                MyLine line1 = obj as MyLine;
                if (bx == true)
                {
                    if (GetMaxPx() < line1.GetMaxPx())
                    {
                        return -1;
                    }
                    else
                    {
                        if (GetMaxPx() == line1.GetMaxPx())
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
                else
                {
                    if (GetMaxPy() < line1.GetMaxPy())
                    {
                        return -1;
                    }
                    else
                    {
                        if (GetMaxPy() == line1.GetMaxPy())
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
            }
            throw new NotImplementedException();
        }
    }
}
